/*	
 *				
 *					ISNW ---- It is a war.It is not warcraft!
 *
 *
 *					Author:					zhoukeli(haxixi_keli)
 *					Filename:				isnwColor.h
 *					CreateTime:				2008/12/15
 *					ModifyTime:				2008/12/15
 *
 */

#ifndef __ISNW_COLOR_H__
#define __ISNW_COLOR_H__

#include "isnwTypes.h"
#include "isnwMath.h"

namespace ISNW
{
namespace video
{
	class color32
	{
	public:
		//! constructor
		color32():c(0){}
		color32(const color32& other):c(other.c){}
		color32(u32 c1):c(c1){}
		color32(u32 red,u32 green,u32 blue){set(red,green,blue);}
		color32(u32 red,u32 green,u32 blue,u32 alpha){set(red,green,blue,alpha);}

	public:
		//! operator
		color32& operator = (const color32& other){c = other.c;return *this;}

		bool operator == (const color32& other) const{return c == other.c;}
		bool operator != (const color32& other) const{return !(*this == other);}

	public:
		//! function
		color32& set(u32 red,u32 green,u32 blue)
		{
			u32 alpha = 0x00ff;
			set(red,green,blue,alpha);

			return *this;
		}

		color32& set(u32 red,u32 green,u32 blue,u32 alpha)
		{
			red = core::_clamp_<u32>(red,0,255);
			green = core::_clamp_<u32>(green,0,255);
			blue = core::_clamp_<u32>(blue,0,255);

			c = (alpha & 0x00ff) << 24 | (red & 0x00ff) << 16 | (green & 0x00ff) << 8 | (blue & 0x00ff);

			return *this;
		}

		color32& set(const color32& other)
		{
			c = other.c;

			return *this;
		}

		color32& set(u32 color)
		{
			c = color;

			return *this;
		}

		// 5-5-5
		color32& set555(u16 color)
		{
			set(color&0x7c00,color&0x03e0,color&0x001f);
		};

		// 5-6-5
		color32& set565(u16 color)
		{
			set(color&0xf800,color&0x07e0,color&0x001f);
		}

		u32 getred() const
		{
			return (c & 0x00ff0000) >> 16;
		}

		u32 getgreen() const
		{
			return (c & 0x0000ff00) >> 8;
		}

		u32 getblue() const
		{
			return (c & 0x000000ff);
		}

		u32 getalpha() const
		{
			return (c & 0xff000000) >> 24;
		}

		color32& setred(u32 red)
		{
			u8 r = (u8)(red & 0x00ff);
			// first,clear red
			c &= 0xff00ffff;
			// then,reset red
			c |= (u32)(r << 16) & 0x00ff0000;

			return *this;
		}

		color32& setgreen(u32 green)
		{
			u8 g = (u8)(green & 0x00ff);
			// clear green
			c &= 0xffff00ff;
			// reset green
			c |= (u32)(g << 8) & 0x0000ff00;

			return *this;
		}

		color32& setblue(u32 blue)
		{
			// clear blue
			c &= 0xffffff00;
			// reset blue
			c |= (blue & 0x000000ff);

			return *this;
		}

		color32& setalpha(u32 alpha)
		{
			u8 a = (u8)(alpha & 0x00ff);
			// clear alpha
			c &= 0x00ffffff;
			// reset alpha
			c |= (u32)(a << 24) & 0xff000000;

			return *this;
		}

		f32 getLuminance() const
		{
			return (0.3f * getred() + 0.59f * getgreen() + 0.11f * getblue());
		}

	public:
		u32 c;
	};

} // end namespace video
} // end namespace ISNW
#endif